<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['compensationManagement:setRetireeSubsidyItems:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" border stripe :data="setRetireeSubsidyItemsList">
      <el-table-column label="补贴项目id" align="center" prop="subsidyprojectId"/>
      <el-table-column label="补贴项目名称" align="center" prop="subsidyprojectName"/>
      <el-table-column label="补贴项目金额" align="center" prop="subsidyprojectMoney"/>
      <el-table-column label="补贴项目类型" align="center" prop="subsidyprojectType">
        <template slot-scope="{row}">
          <el-tag
            type="success"
            v-show="row.subsidyprojectType==='1'"
          > 固定项目
          </el-tag>
          <el-tag
            type="primary"
            v-show="row.subsidyprojectType==='2'"
          > 浮动项目
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="补贴项目备注" align="center" prop="subsidyprojectRemarks"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['compensationManagement:setRetireeSubsidyItems:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['compensationManagement:setRetireeSubsidyItems:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改退休人员补贴项目设置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-tooltip v-if="form.subsidyprojectId==null" class="item" effect="dark" content="固定项目已固定无法添加，请添加浮动项目" placement="top-start">
          <el-form-item label="补贴项目名称" prop="subsidyprojectType">
            <el-radio-group v-model="form.subsidyprojectType">
              <el-radio :label="'2'">浮动项目</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-tooltip>

        <el-row>
          <el-col :span="12">
            <el-form-item label="补贴项目名称" prop="subsidyprojectName">
              <el-input v-model="form.subsidyprojectName" :disabled="form.subsidyprojectType=='1'" placeholder="请输入补贴项目名称"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="18">
            <el-form-item label="补贴项目金额" prop="subsidyprojectMoney">
              <el-input-number v-model="form.subsidyprojectMoney" controls-position="right" :precision="2" :min="0"
                               :step="0.1"
              ></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="补贴项目备注" prop="subsidyprojectRemarks">
              <el-input v-model="form.subsidyprojectRemarks" type="textarea" placeholder="请输入补贴项目备注"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSetRetireeSubsidyItems,
  getSetRetireeSubsidyItems,
  delSetRetireeSubsidyItems,
  addSetRetireeSubsidyItems,
  updateSetRetireeSubsidyItems
} from '@/api/compensationManagement/setRetireeSubsidyItems'

export default {
  name: 'SetRetireeSubsidyItems',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 退休人员补贴项目设置表格数据
      setRetireeSubsidyItemsList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        subsidyprojectType: [{ required: true, message: '项目类型不能为空', trigger: 'change' }],
        subsidyprojectName: [{ required: true, message: '补贴项目名称不能为空', trigger: 'blur' }],
        subsidyprojectMoney: [{ required: true, message: '补贴项目金额不能为空', trigger: 'blur' }]

      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询退休人员补贴项目设置列表 */
    getList() {
      this.loading = true
      listSetRetireeSubsidyItems(this.queryParams).then(response => {
        this.setRetireeSubsidyItemsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        subsidyprojectId: null,
        subsidyprojectName: null,
        subsidyprojectMoney: null,
        subsidyprojectRemarks: null,
        subsidyprojectType: null
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.subsidyprojectId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '设置退休人员补贴项目'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const subsidyprojectId = row.subsidyprojectId || this.ids
      getSetRetireeSubsidyItems(subsidyprojectId).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改退休人员补贴项目设置'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.subsidyprojectId != null) {
            updateSetRetireeSubsidyItems(this.form).then(response => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addSetRetireeSubsidyItems(this.form).then(response => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const subsidyprojectIds = row.subsidyprojectId || this.ids
      this.$modal.confirm('是否确认删除退休人员补贴项目设置编号为"' + subsidyprojectIds + '"的数据项？').then(function() {
        return delSetRetireeSubsidyItems(subsidyprojectIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('compensationManagement/setRetireeSubsidyItems/export', {
        ...this.queryParams
      }, `setRetireeSubsidyItems_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
